昨天用完新增畫面了,今天來將資料存進Realm裡。
首先要先建立Realm的架構,內容大概如下。
import RealmSwift
import Foundation
class AlarmRealm: Object {
@objc dynamic var ID = UUID().uuidString
@objc dynamic var Hour: Int = 0
@objc dynamic var Minute: Int = 0
@objc dynamic var Sunday: Bool = false
@objc dynamic var Monday: Bool = false
@objc dynamic var Tuesday: Bool = false
@objc dynamic var Wednesday: Bool = false
@objc dynamic var Thursday: Bool = false
@objc dynamic var Friday: Bool = false
@objc dynamic var Saturday: Bool = false
@objc dynamic var labelText: String = ""
@objc dynamic var soundSelect: Int = 0
@objc dynamic var switchOnOff: Bool = true
override static func primaryKey() -> String? {
return "ID"
}
}
這裡在儲存Button裡打上要儲存的資料。
let realm = try! Realm()
let inputData = AlarmRealm()
let cells = realm.objects(AlarmRealm.self)
inputData.Hour = addPageValue.pageValue.pickHour
inputData.Minute = addPageValue.pageValue.pickMinute
inputData.Sunday = addPageValue.pageValue.repeatDayCheck[0]
inputData.Monday = addPageValue.pageValue.repeatDayCheck[1]
inputData.Tuesday = addPageValue.pageValue.repeatDayCheck[2]
inputData.Wednesday = addPageValue.pageValue.repeatDayCheck[3]
inputData.Thursday = addPageValue.pageValue.repeatDayCheck[4]
inputData.Friday = addPageValue.pageValue.repeatDayCheck[5]
inputData.Saturday = addPageValue.pageValue.repeatDayCheck[6]
inputData.labelText = addPageValue.pageValue.labelText
inputData.soundSelect = addPageValue.pageValue.soundSelect
inputData.switchOnOff = addPageValue.pageValue.switchOnOff
try! realm.write {
realm.add(inputData)
}
這樣就能將資料存進裡面了,明天就能在顯示畫面裡顯示了。